<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc">

  <xsl:import href="../base.xsl" />

  <xsl:param name="_apply-mode"/>
  
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="$_apply-mode = 'doc-info'">
        <xsl:apply-templates select="." mode="doc-info"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="." mode="default"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/*" mode="doc-info" xmlns="">
    <author><xsl:value-of select="doc:history/doc:record[last()]/@author"/></author>
    <title><xsl:value-of select="doc:head/doc:title"/></title>
    <keywords><xsl:value-of select="doc:head/doc:keywords"/></keywords>
    <xsl:variable name="creation" select="doc:history/doc:record[1]"/>
		<xsl:choose>
			<xsl:when test="$livcos_version &lt; '0.3'">
				<creation author="{$creation/@author}"
				  date="{substring($creation/@time, 1, 10)}" time="{substring($creation/@time, 12, 8)}"/>
			</xsl:when>
			<xsl:otherwise>
				<creation author="{$creation/@author}" time="{$creation/@time}" />
			</xsl:otherwise>
		</xsl:choose>
  </xsl:template>

  <!-- unrecognized elements -->

  <xsl:template match="text()">
    <xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="@*">
    <xsl:text> </xsl:text>
    <xsl:value-of select="name(.)" />
    <xsl:text>="</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>"</xsl:text>
  </xsl:template>

  <xsl:template match="*">
    <block color="red">
      <xsl:text><![CDATA[<]]></xsl:text>
      <xsl:value-of select="name(.)" />
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="namespace::*[local-name(.) != 'xml']">
        <xsl:variable name="prefix" select="local-name(.)"/>
        <xsl:variable name="nsUri" select="."/>
        <xsl:if test="not(../../namespace::*[(local-name(.) = $prefix) and (. = $nsUri)])">
          <xsl:text> xmlns</xsl:text>
          <xsl:if test="$prefix != ''">:</xsl:if>
          <xsl:value-of select="$prefix" />="<xsl:value-of select="$nsUri" />
          <xsl:text>"</xsl:text>
        </xsl:if>
      </xsl:for-each>
      <xsl:choose>
        <xsl:when test="node()">
          <xsl:text><![CDATA[>]]></xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>/<![CDATA[>]]></xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </block>
    <xsl:if test="node()">
      <xsl:apply-templates select="node()"/>
      <block color="red">
        <xsl:text><![CDATA[<]]>/</xsl:text>
        <xsl:value-of select="name(.)" />
        <xsl:text><![CDATA[>]]></xsl:text>
      </block>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/*">
    <root>
    
      <layout-master-set>
        <simple-page-master master-name="page_A4" page-height="29.7cm" page-width="21cm"
          margin-top="1.5cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
          <region-body margin-top="2cm" margin-bottom="1cm" margin-left="0.5cm"
            margin-right="0.5cm" />
          <region-before extent="2cm" />
          <region-after extent="1cm" />
        </simple-page-master>
      </layout-master-set>

      <page-sequence master-reference="page_A4">
        <flow flow-name="xsl-region-body" font-size="10pt" font-family="Verdana">
          <block margin-top="18pt" margin-bottom="16pt" font-size="24pt">
            <xsl:text><![CDATA[<]]></xsl:text>
            <xsl:value-of select="name(.)" />
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="namespace::*[local-name(.) != 'xml']">
              <xsl:variable name="prefix" select="local-name(.)"/>
              <xsl:variable name="nsUri" select="."/>
              <xsl:if test="not(../../namespace::*[(local-name(.) = $prefix) and (. = $nsUri)])">
                <xsl:text> xmlns</xsl:text>
                <xsl:if test="$prefix != ''">:</xsl:if>
                <xsl:value-of select="$prefix" />="<xsl:value-of select="$nsUri" />
                <xsl:text>"</xsl:text>
              </xsl:if>
            </xsl:for-each>
            <xsl:text><![CDATA[>]]></xsl:text>
          </block>
          <xsl:apply-templates select="*"/>
        </flow>
      </page-sequence>
      
    </root>
  </xsl:template>

</xsl:stylesheet>
